//
//  HLControl.m
//  WNSDKTools
//
//  Created by 胡林虎 on 16/7/4.
//  Copyright © 2016年 胡林虎. All rights reserved.
//

#import "HLControl.h"

@implementation HLControl
#pragma mark - For UIView
+ (UIView *)createUIViewWithFrame:(CGRect)frame
                          bgColor:(UIColor *)bgColor
                              tag:(NSInteger)tag

{
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = bgColor;
    view.tag = tag;
    return view;
}

+ (UIView *)createUIViewWithFrame:(CGRect)frame
                          bgColor:(UIColor *)bgColor
                     cornerRadius:(CGFloat)cornerRadius
                              tag:(NSInteger)tag
{
    UIView *view = [self createUIViewWithFrame:frame bgColor:bgColor tag:tag];
    if (cornerRadius > 0) {
        view.clipsToBounds = YES;
        view.layer.cornerRadius = cornerRadius;
    }
    return view;
}

+ (UIView *)createUIViewWithFrame:(CGRect)frame
                          bgColor:(UIColor *)bgColor
                     cornerRadius:(CGFloat)cornerRadius
                    actionGesture:(UIGestureRecognizer *)gesture
                              tag:(NSInteger)tag
{
    UIView *view = [self createUIViewWithFrame:frame bgColor:bgColor cornerRadius:cornerRadius tag:tag];
    [view addGestureRecognizer:gesture];
    return view;
}

+ (UIView *)createUIViewWithFrame:(CGRect)frame
                          bgColor:(UIColor *)bgColor
                     cornerRadius:(CGFloat)cornerRadius
                              tag:(NSInteger)tag
                        tapAction:(void(^)())tapAction
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
        if (tapAction) {
            tapAction();
        }
    }];
    UIView *view = [self createUIViewWithFrame:frame bgColor:bgColor cornerRadius:cornerRadius actionGesture:tap tag:tag ];
    return view;
}

#pragma mark - For UILabel
+ (UILabel *)createLabelWithFrame:(CGRect)frame
                             text:(NSString *)text
                    textAlignment:(NSTextAlignment)textAlignment
                         fontSize:(CGFloat)fontSize
                              tag:(NSInteger)tag
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.adjustsFontSizeToFitWidth = YES;
    label.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    label.tag = tag;
    if (textAlignment) {
        label.textAlignment = textAlignment;
    }
    if (fontSize > 0) {
        label.font = [UIFont systemFontOfSize:fontSize];
    }
    return label;
}

+ (UILabel *)createLabelWithFrame:(CGRect)frame
                             text:(NSString *)text
                    textAlignment:(NSTextAlignment)textAlignment
                         fontSize:(CGFloat)fontSize
                        textColor:(UIColor *)textColor
                              tag:(NSInteger)tag

{
    UILabel *label = [self createLabelWithFrame:frame text:text  textAlignment:textAlignment fontSize:fontSize tag:tag];
    label.textColor = textColor;
    return label;
}

+ (UILabel *)createLabelWithFrame:(CGRect)frame
                             text:(NSString *)text
                    textAlignment:(NSTextAlignment)textAlignment
                         fontSize:(CGFloat)fontSize
                        textColor:(UIColor *)textColor
                          bgColor:(UIColor *)bgColor
                              tag:(NSInteger)tag
{
    UILabel *label = [self createLabelWithFrame:frame text:text  textAlignment:textAlignment fontSize:fontSize textColor:textColor tag:tag];
    label.backgroundColor = bgColor;
    return label;
}

+ (UILabel *)createLabelWithFrame:(CGRect)frame
                             text:(NSString *)text
                    textAlignment:(NSTextAlignment)textAlignment
                         fontSize:(CGFloat)fontSize
                        textColor:(UIColor *)textColor
                          bgColor:(UIColor *)bgColor
                     cornerRadius:(CGFloat)cornerRadius
                              tag:(NSInteger)tag

{
    UILabel *label = [self createLabelWithFrame:frame text:text  textAlignment:textAlignment fontSize:fontSize textColor:textColor bgColor:bgColor tag:tag];
    if (cornerRadius > 0) {
        label.clipsToBounds = YES;
        label.layer.cornerRadius = cornerRadius;
    }
    return label;
}

+ (UILabel *)createLabelWithFrame:(CGRect)frame
                             text:(NSString *)text
                    textAlignment:(NSTextAlignment)textAlignment
                         fontSize:(CGFloat)fontSize
                        textColor:(UIColor *)textColor
                          bgColor:(UIColor *)bgColor
                     cornerRadius:(CGFloat)cornerRadius
                              tag:(NSInteger)tag
                        tapAction:(void(^)())tapAction
{
    UILabel *label = [self createLabelWithFrame:frame text:text  textAlignment:textAlignment fontSize:fontSize textColor:textColor bgColor:bgColor cornerRadius:cornerRadius tag:tag];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
        if (tapAction) {
            tapAction();
        }
    }];
    label.userInteractionEnabled = YES;
    [label addGestureRecognizer:tap];
    return label;
}

#pragma mark - For UIButton
+ (UIButton *)createButtonWithFrame:(CGRect)frame
                              title:(NSString *)title
                           fontSize:(CGFloat)fontSize
                          imageName:(NSString *)imageName
                        bgImageName:(NSString *)bgImageName
                                tag:(NSInteger)tag
                             action:(void(^)())action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    button.tag = tag;
    if (title) {
        [button setTitle:title forState:UIControlStateNormal];
    }
    if (fontSize > 0) {
        button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    }
    if (imageName) {
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    if (bgImageName) {
        [button setBackgroundImage:[UIImage imageNamed:bgImageName] forState:UIControlStateNormal];
    }
    [button addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
        if (action) {
            action();
        }
    }];
    return button;
}

+ (UIButton *)createButtonWithFrame:(CGRect)frame
                              title:(NSString *)title
                           fontSize:(CGFloat)fontSize
                         titleColor:(UIColor *)titleColor
                            bgColor:(UIColor *)bgColor
                          imageName:(NSString *)imageName
                        bgImageName:(NSString *)bgImageName
                                tag:(NSInteger)tag
                             action:(void(^)())action
{
    
    UIButton *button = [self createButtonWithFrame:frame title:title fontSize:fontSize imageName:imageName bgImageName:bgImageName tag:tag action:action];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.backgroundColor = bgColor;
    return button;
}

+ (UIButton *)createButtonWithFrame:(CGRect)frame
                              title:(NSString *)title
                           fontSize:(CGFloat)fontSize
                         titleColor:(UIColor *)titleColor
                            bgColor:(UIColor *)bgColor
                          imageName:(NSString *)imageName
                        bgImageName:(NSString *)bgImageName
                       cornerRadius:(CGFloat)cornerRadius
                                tag:(NSInteger)tag
                             action:(void(^)())action
{
    UIButton *button = [self createButtonWithFrame:frame title:title fontSize:fontSize titleColor:titleColor bgColor:bgColor imageName:imageName bgImageName:bgImageName tag:tag action:action];
    if (cornerRadius > 0) {
        button.clipsToBounds = YES;
        button.layer.cornerRadius = cornerRadius;
    }
    return button;
}

#pragma mark - For UIImageView
+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
                                imageName:(NSString *)imageName
                                      tag:(NSInteger)tag
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.image = [UIImage imageNamed:imageName];
    imageView.tag = tag;
    return imageView;
}

+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
                             cornerRadius:(CGFloat)cornerRadius
                                      tag:(NSInteger)tag
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.tag = tag;
    if (cornerRadius > 0) {
        imageView.clipsToBounds = YES;
        imageView.layer.cornerRadius = cornerRadius;
        imageView.layer.masksToBounds = YES;
    }
    return imageView;
}

+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
                                imageName:(NSString *)imageName
                             cornerRadius:(CGFloat)cornerRadius
                                      tag:(NSInteger)tag
{
    UIImageView *imageView = [self createImageViewWithFrame:frame cornerRadius:cornerRadius tag:tag];
    imageView.image = [UIImage imageNamed:imageName];
    return imageView;
}

+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
                                imageName:(NSString *)imageName
                             cornerRadius:(CGFloat)cornerRadius
                                      tag:(NSInteger)tag
                            actionGesture:(UIGestureRecognizer *)gesture
{
    UIImageView *imageView = [self createImageViewWithFrame:frame imageName:imageName cornerRadius:cornerRadius tag:tag];
    imageView.userInteractionEnabled = YES;
    [imageView addGestureRecognizer:gesture];
    return imageView;
}

+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
                                imageName:(NSString *)imageName
                             cornerRadius:(CGFloat)cornerRadius
                                      tag:(NSInteger)tag
                                tapAction:(void(^)())tapAction
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
        if (tapAction) {
            tapAction();
        }
    }];
    UIImageView *imageView = [self createImageViewWithFrame:frame imageName:imageName cornerRadius:cornerRadius tag:tag actionGesture:tap];
    return imageView;
}

+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
                                imageName:(NSString *)imageName
                              roundCorner:(BOOL)roundCorner
                                      tag:(NSInteger)tag
                                tapAction:(void(^)())tapAction
{
    CGFloat cornerRadius = 0;
    if (roundCorner) {
        cornerRadius = frame.size.width / 2;
    }
    return [self createImageViewWithFrame:frame imageName:imageName cornerRadius:cornerRadius tag:tag tapAction:tapAction];
}

+ (UITextField *)createTextFiledWithFrame:(CGRect)frame
                              bgImageName:(NSString *)bgImageName
                              placeholder:(NSString *)placeholder
                                      tag:(NSInteger)tag
{
    UITextField*textField=[[UITextField alloc]initWithFrame:frame];
    textField.tag = tag;
    if (bgImageName) {
        textField.background=[UIImage imageNamed:bgImageName];
    }
    if (placeholder) {
        textField.placeholder=placeholder;
    }

    return textField;

}

+ (UITextField *)createTextFiledWithFrame:(CGRect)frame
                              bgImageName:(NSString *)bgImageName
                              placeholder:(NSString *)placeholder
                             cornerRadius:(CGFloat)cornerRadius
                                      tag:(NSInteger)tag
{
    UITextField *textField = [self createTextFiledWithFrame:frame bgImageName:bgImageName placeholder:placeholder tag:tag];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    if (cornerRadius > 0) {
        textField.clipsToBounds = YES;
        textField.layer.cornerRadius = cornerRadius;
        textField.layer.masksToBounds = YES;
    }
    return textField;
}

+ (UITextField *)createTextFiledWithFrame:(CGRect)frame
                              bgImageName:(NSString *)bgImageName
                              placeholder:(NSString *)placeholder
                               isPassWord:(BOOL)isPassWord
                             cornerRadius:(CGFloat)cornerRadius
                                      tag:(NSInteger)tag
{
    UITextField *textField = [self createTextFiledWithFrame:frame bgImageName:bgImageName placeholder:placeholder cornerRadius:cornerRadius tag:tag];
    if (isPassWord) {
        textField.secureTextEntry = YES;
    }
    return textField;
}

+ (UITextField *)createTextFiledWithFrame:(CGRect)frame
                              bgImageName:(NSString *)bgImageName
                              placeholder:(NSString *)placeholder
                               isPassWord:(BOOL)isPassWord
                                 leftView:(UIView *)leftView
                                rightView:(UIView *)rightView
                             cornerRadius:(CGFloat)cornerRadius
                                      tag:(NSInteger)tag
{
    UITextField *textField = [self createTextFiledWithFrame:frame bgImageName:bgImageName placeholder:placeholder isPassWord: isPassWord cornerRadius:cornerRadius tag:tag];
   
    if (leftView) {
        textField.leftView=leftView;
        textField.leftViewMode=UITextFieldViewModeAlways;
    }
    if (rightView) {
        textField.rightView=rightView;
        textField.rightViewMode=UITextFieldViewModeAlways;
    }
    return textField;
}

@end


static const int target_key;

@implementation UIGestureRecognizer (Gesture)

+(instancetype)initWithActionBlock:(GestureAction)gestureAction
{
    return [[self alloc]initWithActionBlock:gestureAction];
}
- (instancetype)initWithActionBlock:(GestureAction)block {
    self = [self init];
    [self addActionBlock:block];
    [self addTarget:self action:@selector(invoke:)];
    return self;
}
- (void)addActionBlock:(GestureAction)block {
    if (block) {
        objc_setAssociatedObject(self, &target_key, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
}
- (void)invoke:(id)sender {
    GestureAction block = objc_getAssociatedObject(self, &target_key);
    if (block) {
        block(sender);
    }
}
@end

